package com.yxuan.springboot.controller;

import com.yxuan.springboot.model.TestRecords;
import com.yxuan.springboot.service.TestAnswerService;
import com.yxuan.springboot.service.TestOptionsService;
import com.yxuan.springboot.service.TestRecordsService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author zchao
 * TestRecords控制器
 * @date 2023-05-16 14:22:56
 */
@Controller
@Slf4j
@Api(tags = "")
@RequestMapping(value = "/testrecords", method = {RequestMethod.POST, RequestMethod.GET})
public class TestRecordsController extends BaseController<TestRecords> {

    @Autowired
    private TestAnswerService testAnswerService;

    @Autowired
    private TestRecordsService testRecordsService;

    @Autowired
    private TestOptionsService testOptionsService;

    //    提交测试，创建测试记录
    @RequestMapping("/addtestrecord")
    @ResponseBody
    public HashMap<String, Object> createRecords(@RequestBody Map map) throws Exception {
        try {
            TestRecords testRecords = new TestRecords();
            testRecords.setUserId((String) map.get("user_id"));
            testRecords.setTypeId((Integer) map.get("type_id"));
            testRecords.setVersion((String) map.get("version"));
            testRecords.setStatus((String) map.get("status"));
            return testRecordsService.createRecords(testRecords, (List<Map>) map.get("list"));
        } catch (Exception e) {
            HashMap<String, Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result", ERROR);
            return result;
        }
    }

    //    查看所有测试结果,
    @RequestMapping("/allrecords")
    @ResponseBody
    public HashMap<String, Object> seeAllRecords(@RequestBody Map map) throws Exception {
        try {
            int page = (int) map.get("page");
            int limit = (int) map.get("limit");
            TestRecords entity = new TestRecords();
            entity.setUserId((String) map.get("user_id"));
            HashMap<String, Object> listByPramas = (HashMap<String, Object>) testRecordsService.findListByPramas(page, limit, entity);
            return listByPramas;
        } catch (Exception e) {
            HashMap<String, Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result", ERROR);
            return result;
        }

    }

    //    根据user和测试记录id查询该测试的详情
    @RequestMapping("/recorddetail")
    @ResponseBody
    public HashMap<String, Object> recordDetail(@RequestBody Map map) throws Exception {
        try {
            TestRecords entity = new TestRecords();
            entity.setId((Integer) map.get("recordid"));
            entity.setUserId((String) map.get("userid"));
            return testRecordsService.recordDetail(entity);
        } catch (Exception e) {
            HashMap<String, Object> result = new HashMap<>();
            log.error(e.getMessage());
            result.put("result", ERROR);
            return result;
        }
    }
}
